import plugin from '../lib/plugins/plugin.js'
import { segment } from "oicq"
import { createRequire } from 'module'; 
import { setMaxIdleHTTPParsers } from 'http';
const require = createRequire(import.meta.url);
const { exec } = require('child_process')

export class example extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: 'AI画画',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 5000,
      rule: [
        {
          /** 命令正则匹配 */
          reg: '.nai (.*)',
          /** 执行方法 */
          fnc: 'naidraw'
        }
      ]
    })
  }

  /**
   * # ai画画
   * @param e oicq传递的事件参数e
   */
  async naidraw (e) {
    /** e.msg 用户的命令消息 */
    logger.info('[AI-DRAW]recv <= ', e.msg.replace('.nai ', ''))
    var cmdStr = 'python ./plugins/ai-draw/draw.py --tag "' + e.msg.replace('.nai ', '') + '"'
    exec(cmdStr,  async function (error, stdout, stderr) {
      if (error) {
        console.log("获取失败", stderr)
        e.reply(["获取失败，失败原因:" + stderr])
      } else {
        console.log("获取成功", 'plugins/ai-draw/image/')
        e.reply(segment.image('plugins/ai-draw/image/image.png'))
      }
    })
  }
}
